package com.gmics.unifiq.menu;

import java.util.ArrayList;

import com.gmics.android.R;
import com.gmics.unifiq.ViewEvent;
import com.gmics.unifiq.ViewOpenEvent;
import com.gmics.unifiq.adapter.NotificationAdapter;
import com.gmics.unifiq.controller.EntityController;
import com.gmics.unifiq.entity.Event;
import com.gmics.unifiq.entity.OpenEvent;
import com.gmics.unifiq.entity.User;
import com.gmics.unifiq.entity.Notification;
import com.gmics.unifiq.entity.UserNotification;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class Notifications extends Activity {
	private ListView notificationLV;
	private ProgressDialog pDialog = null;
	private User user;
	private EntityController controller = new EntityController();
	private ArrayList<Notification> notificationArr = new ArrayList<Notification>();
	private ArrayList<UserNotification> unArr = new ArrayList<UserNotification>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notification_layout);
		
		Bundle b = getIntent().getExtras();
		user = (User) b.getSerializable("user");
		
	    if (android.os.Build.VERSION.SDK_INT > 9) {
	        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
	        StrictMode.setThreadPolicy(policy);
	      }
	    
		new LoadNotification().execute();
	}
	
    class LoadNotification extends AsyncTask<String, String, String> {
   	 
        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Notifications.this);
            pDialog.setMessage("Loading notifications...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }
 

        protected String doInBackground(String... args) {
        	unArr = controller.retrieveUserNotification(user.getId());
        	ArrayList<Integer> notificationIDArr = new ArrayList<Integer>();
        	
        	for (int i = 0; i < unArr.size(); i++){
        		notificationIDArr.add(unArr.get(i).getNotificationID());
        	}
        	
        	notificationArr = controller.retrieveNotification(notificationIDArr);
        	
        	ArrayList<ArrayList<Integer>> idArr = controller.retrieveOpenEventAttendance(0, user.getId());
        	ArrayList<OpenEvent> oeArr = controller.retrieveOpenEventByID(idArr.get(0));
        	ArrayList<Integer> grpIDArr = controller.retrieveUserGroupID(user.getId());
    		ArrayList<Event> eventArr = controller.retrieveEventByGroupID(grpIDArr);
    		
        	for (int a = 0; a < notificationArr.size(); a++){
        		if (notificationArr.get(a).getOpenEventID() != 0){
        			for (int b = 0; b < oeArr.size(); b++){
        				if (notificationArr.get(a).getOpenEventID() == oeArr.get(b).getId()){
        					String original = notificationArr.get(a).getOriginal();
        					if (!original.equals("Unlocked Avatar") && !original.equals("Obtained Loyalty Points")){
	        					notificationArr.get(a).setName(oeArr.get(b).getName());
        					}
        					else{
        						notificationArr.get(a).setName("");
        					}
        					break;
        				}
        			}
        		}
        		else if (notificationArr.get(a).getEventID() != 0 ){
        			for (int b = 0; b < eventArr.size(); b++){
        				if (notificationArr.get(a).getEventID() == eventArr.get(b).getId()){
        					String original = notificationArr.get(a).getOriginal();
        					if (!original.equals("Unlocked Avatar") && !original.equals("Obtained Loyalty Points")){
	        					notificationArr.get(a).setName(eventArr.get(b).getName());
        					}
        					else{
        						notificationArr.get(a).setName("");
        					}
        					break;
        				}
        			}
        		}
        		else{
        			notificationArr.get(a).setName("");
        		}
        	}
        	
        	controller.updateUserNotification(user.getId());
            return null;
        }
 
        protected void onPostExecute(String file_url) {
            pDialog.dismiss();
            runOnUiThread(new Runnable() {
                public void run() {

            		notificationLV = (ListView) findViewById(R.id.notification_lv);
            		final NotificationAdapter nAdapter = new NotificationAdapter(Notifications.this, notificationArr);
            		notificationLV.setAdapter(nAdapter);
            		
            		notificationLV.setOnItemClickListener(new OnItemClickListener(){

						@Override
						public void onItemClick(AdapterView<?> av, View v,
								int pos, long id) {
							// TODO Auto-generated method stub
							Notification n = (Notification) nAdapter.getItem(pos);
							Bundle b = new Bundle();
							if (n.getOpenEventID() != 0){
								Intent openEventScreen = new Intent(Notifications.this, ViewOpenEvent.class);
								b.putSerializable("selectedOpenEvent", controller.retrieveOpenEventByID(n.getOpenEventID()));
								openEventScreen.putExtras(b);
								startActivity(openEventScreen);
							}
							else if (n.getEventID() != 0){
								Intent eventScreen = new Intent(Notifications.this, ViewEvent.class);
								b.putSerializable("event", controller.retrieveEventByID(n.getEventID()));
								eventScreen.putExtras(b);
								startActivity(eventScreen);
							}
							else{
								Intent changeAvatarScreen = new Intent(Notifications.this, ChangeAvatar.class);
								startActivity(changeAvatarScreen);
							}
						}
            			
            		});
                }
            });
            
        }
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
